﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using NLog;

    public class CDSLCKDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        private bool Delete(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_KeyCode), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDataSet(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VT_Thua_Get");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "VT_Thua_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public bool Save(string ps_Subsidiary, object pd_Ngay_Ct2, DataSet po_DataSet)
        {
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            foreach (DataRow row in po_DataSet.Tables[0].Rows)
            {
                if (((decimal) row["Ton_Cuoi"]) != 0M)
                {
                    if (string.IsNullOrEmpty(row["Stt"].ToString()))
                    {
                        this.SaveRow(SSConnection, transaction, true, ps_Subsidiary, pd_Ngay_Ct2, row);
                    }
                    else
                    {
                        this.SaveRow(SSConnection, transaction, false, ps_Subsidiary, pd_Ngay_Ct2, row);
                    }
                    continue;
                }
                if (!string.IsNullOrEmpty(row["Stt"].ToString()))
                {
                    this.Delete(SSConnection, transaction, row["Stt"]);
                }
            }
            transaction.Commit();
            SSDataAccess.CloseConnection(SSConnection);
            return true;
        }

        private int SaveRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, object pd_Ngay_Ct2, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_DvCs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Ma_Yt", row["Ma_Yt"]), new SqlParameter("p_Ma_Vt_Nh", row["Ma_Vt_Nh"]), new SqlParameter("p_Ma_Dm_Sp", row["Ma_Dm_Sp"]), new SqlParameter("p_Ton_Cuoi", row["Ton_Cuoi"]), new SqlParameter("p_Du_Cuoi", row["Du_Cuoi"]), new SqlParameter("p_Loai_DD", "1"), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[11].Direction = ParameterDirection.Output;
            values[11].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[11].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

